<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png"
             alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info"
                 @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="collapse?'64px':'200px'">
        <div class='open'
             @click="toggleLab">折叠&展开</div>
        <el-menu background-color="#d3dce6"
                 text-color="#fff"
                 active-text-color="#409eff"
                 unique-opened
                 :collapse='collapse'
                 :collapse-transition='false'
                 router
                 :default-active="activepath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''"
                      v-for='item in menuList'
                      :key='item.id'>
            <template slot="title">
              <i :class="iconobj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item-group>
              <el-menu-item :index="'/'+menuitem.path"
                            v-for='menuitem in item.children'
                            :key='menuitem.id'
                            @click="saveMenupath('/'+menuitem.path)">
                <i class="el-icon-menu"></i>
                <span>{{menuitem.authName}}</span>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      menuList: [],
      iconobj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-success',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      collapse: false,
      activepath: ''
    }
  },
  created () {
    this.getMenu()
    this.activepath = window.sessionStorage.getItem('activepath')
  },
  methods: {
    logout: function () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取侧边栏数据
    getMenu: function () {
      const that = this
      this.$http.get('menus').then(function (response) {
        that.menuList = response.data.data
      }, function (err) {
        console.log(err)
      })
    },
    toggleLab: function () {
      this.collapse = !this.collapse
    },
    saveMenupath: function (activemenu) {
      window.sessionStorage.setItem('actviepath', activemenu)
      this.activepath = activemenu
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  display: flex;
  background-color: #b3c0d1;
  color: #333;
  justify-content: space-between;
  align-items: center;
  line-height: 60px;
  color: #fff;
  div {
    display: flex;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}
.el-container {
  height: 100%;
}
.open {
  color: #fff;
  height: 25px;
  line-height: 25px;
  font-size: 12px;
  cursor: pointer;
}
</style>
